package my.weather;

import my.climbing.R;
import android.app.Activity;
import android.content.*;
import android.graphics.*;
import android.graphics.Region.Op;

import android.graphics.drawable.*;

import android.os.Bundle;
import android.view.*;
import android.view.ViewGroup.LayoutParams;

import android.view.ViewGroup.OnHierarchyChangeListener;
import android.view.animation.AnimationUtils;
import android.widget.*;

public class BuckhansanInfo extends Activity implements
		AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
	public int selectedIndex = -1;
	ImageAdapter ia;

	private static int[] IMAGE_SIZE = { 100, 70, 50, 30, 20 };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.hukhan);

		ImageView mtkiv = (ImageView) findViewById(R.id.mtk_img);
		mtkiv.setImageResource(R.drawable.hukhan);
		ia = new ImageAdapter(this);
		Gallery g = (Gallery) findViewById(R.id.gallery);
		g.setCallbackDuringFling(true);
		g.setAdapter(ia);
		g.setOnItemSelectedListener(this);

		g.setOnHierarchyChangeListener(new OnHierarchyChangeListener() {

			@Override
			public void onChildViewRemoved(View parent, View child) {

			}

			@Override
			public void onChildViewAdded(View parent, View child) {

				child.invalidate();
			}
		});

	}

	public void onItemSelected(AdapterView parent, View v, int position, long id) {

		ia.setSelectedPosition(position);
		ia.notifyDataSetChanged();
	}

	public void onNothingSelected(AdapterView parent) {

	}

	public View makeView() {
		ImageView i = new ImageView(this);

		i.setBackgroundColor(0x00000000);
		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		i.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return i;
	}

	public class ImageAdapter extends BaseAdapter {
		int mSelectedPosition = -1;
		ImageView mSelectedImageView = null;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView != null) {
				imageView = (ImageView) convertView;
			} else {
				imageView = new ImageView(mContext);
			}

			imageView.setImageResource(mThumbIds[position]);
			imageView.setAdjustViewBounds(false);

			if (position == mSelectedPosition) {
				imageView.setLayoutParams(new Gallery.LayoutParams(300, 300));
			} else if (Math.abs(position - mSelectedPosition) == 1
					&& mSelectedPosition != -1) {
				imageView.setLayoutParams(new Gallery.LayoutParams(150, 150));

			} else if (Math.abs(position - mSelectedPosition) >= 2
					&& mSelectedPosition != -1) {
				int size = 0;
				if (Math.abs(position - mSelectedPosition) >= IMAGE_SIZE.length) {
					size = IMAGE_SIZE[IMAGE_SIZE.length - 1];
				} else {
					size = IMAGE_SIZE[Math.abs(position - mSelectedPosition)];
				}

				imageView.setLayoutParams(new Gallery.LayoutParams(size, size));

				if (mSelectedPosition < position) {
					Bitmap tmp = ((BitmapDrawable) imageView.getDrawable())
							.getBitmap();
					Bitmap bmp = tmp.copy(tmp.getConfig(), true);
					Bitmap newBmp = cropImage(bmp, 0, 0, 15, size, size, size);
					imageView.setImageBitmap(newBmp);
					bmp.recycle();
				}

			} else {
				imageView.setLayoutParams(new Gallery.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			}

			imageView.setScaleType(ImageView.ScaleType.FIT_XY);

			return imageView;
		}

		private Context mContext;

		public void setSelectedPosition(int p) {
			mSelectedPosition = p;
		}
	}

	private Bitmap cropImage(Bitmap bmp, int cropLeft, int cropTop,
			int cropRight, int cropBottom, int width, int height) {
		Bitmap newBmp = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(newBmp);

		canvas.drawColor(Color.TRANSPARENT);
		canvas.save();
		canvas.translate(0, 0);
		canvas.clipRect(0, 0, width, height);
		canvas.clipRect(cropLeft, cropTop, cropRight, cropBottom, Op.DIFFERENCE);
		canvas.clipRect(0, 0, width, height);
		canvas.drawBitmap(bmp, new Rect(0, 0, width, height), new Rect(0, 0,
				width, height), new Paint());
		canvas.restore();

		return newBmp;
	}

	private Integer[] mThumbIds = { R.drawable.b1, R.drawable.b2,
			R.drawable.b3, R.drawable.b4, R.drawable.b5 };
	private Integer[] mImageIds = { R.drawable.b1, R.drawable.b2,
			R.drawable.b3, R.drawable.b4, R.drawable.b5 };
}